<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns="http://www.w3.org/1999/html">
<head>
    <meta charset="UTF-8">
    <title>权限列表</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/lib/layui/css/layui.css">
    <link rel="stylesheet" href="/static/css/scroll-bar.css">
    <link rel="stylesheet" href="/static/css/sub-page.css">
    <link rel="stylesheet" href="/static/lib/nprogress/nprogress.css">
</head>
<body>
<div class="ok-body">
    <!--面包屑导航区域-->
    <div class="ok-body-breadcrumb">
            <span class="layui-breadcrumb">
                <a><cite>首页</cite></a>
                <a><cite>会员管理</cite></a>
                <a><cite>权限管理</cite></a>
            </span>
        <a class="layui-btn layui-btn-sm" href="javascript:location.replace(location.href);" title="刷新">
            <i class="layui-icon layui-icon-refresh"></i>
        </a>
    </div>
    <!--工具栏-->
    <okToolbar>
        <button class="layui-btn" id="addPermission">
            <i class="layui-icon">&#xe61f;</i>添加权限
        </button>
        <span>共有数据：<i id="countNum"></i> 条</span>
    </okToolbar>
    <!--数据表格-->
    <table class="layui-hide" id="tableId" lay-filter="tableFilter"></table>

    <input id="hidArea" hidden>
    <input id="reloadBtn" hidden>
</div>
<!--js逻辑-->
<script src="/static/lib/layui/layui.js"></script>
<script src="/static/lib/nprogress/nprogress.js"></script>
<script th:inline="none">
    NProgress.start();
    window.onload = function () {
        NProgress.done();
    };

    layui.config({
        base: '/static/js/'
    }).extend({
        treetable: 'treetable/treetable'
    }).use(['element', 'table', 'form', 'jquery', 'laydate', 'treetable'], function () {
        var element = layui.element;
        var form = layui.form;
        var table = layui.table;
        var $ = layui.jquery;
        var laydate = layui.laydate;
        var treetable = layui.treetable;

        laydate.render({
            elem: '#startTime',
            type: "datetime"
        });

        laydate.render({
            elem: '#endTime',
            type: "datetime"
        });

        var renderTable = function() {
            layer.load(2);
            treetable.render({
                id: 'reloadId',
                treeColIndex: 1,
                treeSpid: -1,
                treeIdName: 'authorityId',
                treePidName: 'parentId',
                elem: '#tableId',
                url: '/sys/permission/findAll',
                page: false,
                cols: [[
                    {type: 'numbers'},
                    // {type:'radio'},
                    {field: 'authName', minWidth: 200, title: '权限名称'},
                    {field: 'menuUrl', title: '菜单url'},
                    {field: 'auth', title: '权限标识'},
                    {field: 'orderNum', width: 80, align: 'center', title: '排序号'},
                    {
                        field: 'isMenu', width: 80, align: 'center', templet: function (d) {
                            if (d.isMenu == 0) {
                                return '<span class="layui-badge layui-bg-gray">按钮</span>';
                            }
                            if (d.parentId == -1) {
                                return '<span class="layui-badge layui-bg-blue">目录</span>';
                            } else {
                                return '<span class="layui-badge-rim">菜单</span>';
                            }
                        }, title: '类型'
                    },
                    {templet: '#operationTpl', width: 120, align: 'center', title: '操作'}
                ]],
                done: function (res, curr, count) {
                    layer.closeAll('loading');
                    $("#countNum").text(count);
                }
            });
        }
        renderTable();

        $("#reloadBtn").click(function () {
            renderTable();
        })

        form.on('submit(search)', function () {
            layer.msg('正在查询，请稍后...');
            return false;
        });

        table.on('tool(tableFilter)', function (obj) {
            var data = obj.data;
            console.log("id值：" + data.id);
            var layEvent = obj.event;
            if (layEvent === 'edit') {
                $("#hidArea").val(JSON.stringify(data));
                layer.open({
                    title: '编辑权限',
                    type: 2,
                    shade: false,
                    maxmin: true,
                    shade: 0.5,
                    area: ['40%', '90%'],
                    content: '/sys/permission/addOrUpdateIndex',
                    zIndex: layer.zIndex,
                    end: function () {
                        console.log("end");
                        $(".layui-laypage-btn")[0].click();
                        $("#hidArea").val(""); //清空回填数据
                    }
                });
            } else if (layEvent === 'del') {
                var id = data.id;
                var isM = data.isMenu;
                console.log(isM)
                var msg;
                if(id==1){
                    layer.alert("系统根目录，禁止删除！",{icon:2})
                    return false;
                }
                if(isM == 1 || isM == 2){
                    msg= '您确定要删除该目录及目录下所有吗！';
                }
                if(isM == 0){
                    msg= '您确定要删除该按钮吗!';
                }

                layer.confirm(msg, {skin: 'layui-layer-lan', icon: 2, title: '提示', anim: 6}, function () {
                    $.ajax({
                        url:'/sys/permission/delByIds',
                        data:{id:id},
                        dataType:'json',
                        type:'post',
                        success:function (res) {
                            renderTable();  //刷新表格
                            layer.msg(res.msg);
                        }
                    })


                });
            }
        });

        $("#batchDel").click(function () {
            layer.confirm("确定要批量删除吗？", {skin: 'layui-layer-lan', icon: 2, title: '提示', anim: 6}, function () {
                var checkStatus = table.checkStatus('tableId');
                var rows = checkStatus.data.length;
                if (rows > 0) {
                    var idsStr = "";
                    for (var i = 0; i < checkStatus.data.length; i++) {
                        idsStr += checkStatus.data[i].id + ',';
                    }
                    console.log("选择的id-->" + idsStr);
                    layer.msg("操作成功！", {icon: 1, time: 1000}, function () {
                        $(".layui-laypage-btn")[0].click();
                    });
                } else {
                    layer.msg('未选择有效数据', {
                        offset: 't',
                        anim: 6
                    });
                }
            });
        })

        $("#addPermission").click(function () {
            layer.open({
                title: '添加权限',
                type: 2,
                shade: false,
                maxmin: true,
                shade: 0.5,
                anim: 4,
                area: ['40%', '90%'],
                content: '/sys/permission/addOrUpdateIndex',
                zIndex: layer.zIndex,
                // skin: 'layui-layer-molv',
                end: function () {
                    $(".layui-laypage-btn")[0].click();
                }
            });
        })
    })
</script>
<!--模板-->
<script type="text/html" id="operationTpl">
    <a href="javascript:;" title="编辑" lay-event="edit"><i class="layui-icon">&#xe642;</i></a>
    <a href="javascript:;" title="删除" lay-event="del"><i class="layui-icon">&#xe640;</i></a>
</script>
</body>
</html>
